﻿using Engine.Upgrade;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameEntry : MonoBehaviour
{
    public bool m_bAssetBundle = false;
    void Awake()
    {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        Application.targetFrameRate = 30;

        SetDirPath();

        GameObject uiroot = LoadUICanvas();
        LoadLaunchPanel(uiroot);
        DontDestroyOnLoad(this);

        Debug.Log("Application.persistentDataPath   " + Application.dataPath);
    }

    void Start()
    {
        EnterGame();
    }

    /// <summary>
    /// Set resource path
    /// </summary>
    void SetDirPath()
    {
        string strResDir = "";
        if (Application.isEditor == false)
        {
            strResDir = Application.persistentDataPath + "/assets/";
        }
        else
        {
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
#if UNITY_EDITOR
                if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
                {
                    strResDir = Application.dataPath + "/../../Resources/android/";
                }
                else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.StandaloneOSXIntel)
                {
                    strResDir = Application.dataPath + "/../../Resources/android/";
                }
                else
                {
#endif
                    strResDir = Application.dataPath + "/../../Resources/win32/";
#if UNITY_EDITOR
                }
#endif
            }
            else
            {
                strResDir = Application.dataPath + "/../../Resources/ios/";
            }
        }

        Utility.Log.Info("设置资源路径:{0}", strResDir);
        Utility.FileUtils.Instance().ResourcePath = strResDir;
    }

    GameObject LoadUICanvas()
    {
        GameObject canvas = Resources.Load("Canvas") as GameObject;
        if (canvas != null)
        {
            GameObject go = GameObject.Instantiate(canvas) as GameObject;
            go.name = "UIRoot";
            go.tag = "UI_Root";

            go.SetActive(true);
            DontDestroyOnLoad(go);
            return go;
        }
        Debug.LogWarning("------->>Don't have canvas in resources folder");
        return null;
    }

    void LoadLaunchPanel(GameObject uiroot)
    {
        GameObject launch = Resources.Load("LaunchPanel") as GameObject;
        if (launch != null)
        {
            Debug.Log("Dont't have launchpanel in resource folder");
        }

        GameObject go = GameObject.Instantiate(launch) as GameObject;
        go.transform.SetParent(uiroot.transform);
        RectTransform rect = go.GetComponent<RectTransform>();
        rect.sizeDelta = Vector2.zero;
        rect.localScale = Vector3.one;
    }

    void EnterGame()
    {
        this.enabled = false;
        this.gameObject.AddComponent<GameMain>();
        this.gameObject.AddComponent<GameConfig>();
        LuaManager.Instance.m_bAssetBundle = m_bAssetBundle;
    }
}
